package T4.EjemplosDelSwing.EjemplosDibujar;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Dibujar1 extends JFrame implements ActionListener {

    JPanel panel = new JPanel();

    public Dibujar1() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);
        setTitle("Dibujo");
        Container contentpane = this.getContentPane();
        contentpane.setLayout(new BorderLayout());
        JPanel panelSur = new JPanel();
        contentpane.add(panelSur, BorderLayout.SOUTH);
        JButton boton = new JButton("Dibujar");
        boton.addActionListener(this);
        boton.setActionCommand("Dibujar");
        panelSur.add(boton);
        JButton boton2 = new JButton("Salir");
        boton2.addActionListener(this);
        boton2.setActionCommand("Salir");
        panelSur.add(boton2);
        contentpane.add(panel, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e) {
        String comando = e.getActionCommand();
        if (comando.equals("Salir")) {
            System.exit(0);
        }
        Graphics2D g2d = (Graphics2D) panel.getGraphics();
        g2d.setColor(Color.blue);
        g2d.drawLine(0, 0, 100, 100);
        g2d.drawLine(150, 150, (int) (this.getSize()).getWidth(), (int) (this.getSize()).getHeight());
        g2d.drawRect(100, 80, 200, 200);
        g2d.setColor(Color.red);
        g2d.fillRect(110, 90, 150, 150);
    }

    public static void main(String[] args) {
        (new Dibujar1()).setVisible(true);
    }
}
